package cz.data.common.database.metadata.parser;

import cz.data.common.exception.DataException;
import cz.data.common.support.metadata.OracleDataTypeEnum;
import cz.data.common.support.ColumnParser;
import cz.data.common.support.DataType;
import cz.data.common.support.DataTypeEnum;

import java.util.Optional;

public class OracleColumnParser implements ColumnParser {

    @Override
    public DataType parse(DataTypeEnum dataTypeEnum) {
        OracleDataTypeEnum typeEnum = OracleDataTypeEnum.match(dataTypeEnum.getValue(), OracleDataTypeEnum.CHAR);
        switch(typeEnum) {
            case CHAR:
                return new OracleCharDataType();
            case VARCHAR:
                return new OracleVarcharDataType();
            case DATE:
            case DATETIME:
                return new OracleDateDataType();
            case NUMBER:
                return new OracleNumberDataType();
            case CLOB:
                return new OracleClobDataType();
            case BLOB:
                return new OracleBlobDataType();
            case TIMESTAMP:
                return new OracleTimestampDataType();
            default:
                throw new DataException("字段数据类型错误");
        }
    }

    public static class OracleBlobDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "BLOB";
        }
    }

    public static class OracleCharDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "VARCHAR2(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class OracleClobDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "CLOB";
        }
    }

    public static class OracleDateDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "DATE";
        }
    }

    public static class OracleNumberDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "NUMBER(" + Optional.ofNullable(columnLength).orElse("22") + ", " + Optional.ofNullable(columnScale).orElse("0") + ")";
        }
    }

    public static class OracleTimestampDataType implements DataType {
        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TIMESTAMP";
        }
    }

    public static class OracleVarcharDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "VARCHAR2(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }
}
